/**
 * losenote.com Inc.
 * Copyright (c) 2013-2013 All Rights Reserved.
 */
package com.losenote.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.losenote.base.LnConstrant;
import com.losenote.base.template.AbstractServiceImpl;
import com.losenote.base.template.BlankCallback;
import com.losenote.core.FindNoteComponent;
import com.losenote.core.MessageComponent;
import com.losenote.core.QuestionComponent;
import com.losenote.enums.MessageTypeEnum;
import com.losenote.enums.QuestionTypeEnum;
import com.losenote.model.FindNote;
import com.losenote.model.Message;
import com.losenote.model.OperateContext;
import com.losenote.model.Question;
import com.losenote.service.FindNoteService;
import com.losenote.service.req.FindNodePubReq;
import com.losenote.service.result.FindNoteResult;
import com.losenote.service.result.LnResult;

/**
 * 招領告示查询服务
 * 
 * @author danjingyu
 * @version $Id: FindNoteQueryServiceImpl.java, v 0.1 2013-10-28 下午10:16:54 danjingyu Exp $
 */
public class FindNoteServiceImpl extends AbstractServiceImpl implements FindNoteService {

    @Autowired
    private FindNoteComponent findNoteComponent;

    @Autowired
    private MessageComponent  messageComponent;

    @Autowired
    private QuestionComponent questionComponent;

    public LnResult publishNote(final FindNodePubReq req, final OperateContext operatorContext) {
        return serviceTemplate.execute(LnResult.class, new BlankCallback() {

            public LnResult executeService() {
                //【1】发布文章
                int id = findNoteComponent.publishNote(req, operatorContext);

                //【2】记录操作日志
                LnResult result = LnResult.retSuccess();
                result.getRelatedAttr().put(LnConstrant.ID, id);

                //【3】返回日志
                return result;
            }

        });
    }

    /**   
     * @see com.losenote.service.FindNoteService#queryById(int)
     */
    @Override
    public FindNoteResult queryById(final int id) {
        return serviceTemplate.executeWithoutTransaction(FindNoteResult.class, new BlankCallback() {

            public FindNoteResult executeService() {
                //【1】查询
                FindNote findNote = findNoteComponent.queryById(id);

                //【2】问题
                List<Question> questions = questionComponent.queryByTypeAndId(id,
                    QuestionTypeEnum.FIND_NOTE);
                //【3】留言信箱
                List<Message> messages = messageComponent.queryMessages(id,
                    MessageTypeEnum.NOTE_LEAVE_MESSAGE);
                //【2】构造结果
                FindNoteResult result = new FindNoteResult(findNote, questions, messages);

                //【3】返回
                return result;
            }

        });
    }

    public void setFindNoteComponent(FindNoteComponent findNoteComponent) {
        this.findNoteComponent = findNoteComponent;
    }

    public void setMessageComponent(MessageComponent messageComponent) {
        this.messageComponent = messageComponent;
    }

}
